/**
 * https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/
 * 将有序数组转换为二叉搜索树
 * @param nums 
 */
const sortedArrayToBST = (nums: number[]): TreeNode | null => {
    const len = nums.length;

    // 数组为空
    if (len === 0) return null

    /**
     * 创建树
     * @param s 
     * @param e 
     */
    const createNode = (s = 0, e = len): TreeNode | null => {
        // 相等直接返回
        if (e === s) return null;
        if (e - s === 1) return new TreeNode(s);

        // 获取中间
        const m = (s + e) >> 1;

        // 构建左边
        const l = createNode(s, m)

        // 构建右边
        const r = createNode(m + 1, e)

        // 创建节点
        let n = new TreeNode(nums[m], l, r)

        return n
    }
    return createNode();
};  